<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

use Illuminate\Database\Eloquent\Builder;
use App\User;
use App\IdentityCard;

Route::get('/', function () {
    $ip = request()->ip();
    if (!strpos($ip,'0.1')) {
        $res = file_get_contents('https://apis.map.qq.com/ws/location/v1/ip?ip=' . $ip . '&key=RPEBZ-J2ZWW-2CIRC-OO3Q2-HH7X2-7GBEJ');
        $arr = json_decode($res, 1)['result'];
        $location = $arr['location'];

        $res2 = file_get_contents('https://apis.map.qq.com/ws/geocoder/v1/?coord_type=5&get_poi=0&output=json&key=VAGBZ-NHYCP-PUKDG-VP6FT-ODHP5-MDBO5&location=' . $location['lat'] . '%2C' . $location['lng']);
        \Illuminate\Support\Facades\DB::table('zhaoshang_log')->insert([
            'date' => date('Y-m-d H:i:s'), 'str' => $res2, 'ip' => $ip,
            'address' => $arr['ad_info']['province'] . $arr['ad_info']['city']
        ]);
    }

    return view('chat');

//    $params = request()->all();
//
//    $a = $params['a'] ?? '';
//    $b = $params['b'] ?? '';
//
//
//    dd($params);
//
//
//    dd($b);

    //map闭包
//    $data = \App\User::query()->get()->map(function ($val) {
//
//        return [
//            'id' => $val->id,
//            'full_name' => $val->email . $val->name,
//            'created_at' => $val->created_at
//        ];
//    });


//    $sql = \App\User::query()->popular()->active()->orderBy('created_at')->get()->toArray();
//    dump($sql);


//    $users = App\User::popular()->orWhere(function (Builder $query) {
//        $query->active();
//    })->get();

//    $users = App\User::popular()->OfType('aaa')->get();
//    $user = User::query()->where('id',11)->update(['name'=>'wangchuan']);
//dump($user);

//    $user = User::with('identity_card')->where('identity_card.city','太原')->get()->toArray();
//    $user = User::query()->where('name','侯金凤')->firstOrFail()->identity_card->where('city','南宁')->get()->toArray();

//    $id_card = IdentityCard::query()->with('user1')->get();

//    $user = App\Admin::query()->where('id',30)->with('roles')->get();
//    $roles = $user->roles;
//    foreach ($user->roles as $role) {
//        dump($role->pivot->admin_id);
//    }

//    $history = \App\Supplier::query()->where('id',1)->first()->userHistory;

//    $user = App\User::find(2);
//    $history = $user->identity_cards();
////    $history2 = User::query()->find(2)->identity_cards;
////    $user = App\User::query()->where('id', 2)->with('identity_cards')->get();;

//    $data = User::query()->select(['name', 'email'])->withCount(['identity_cards' => function (Builder $builder) {
//        $builder->where('type', 2);
//    }])->get();

//    $data = User::query()->select(['name', 'email'])
//        ->withCount(['identity_cards' => fn(Builder $builder) => $builder->where('type', 2)])
//        ->get();


//    $books = User::query()->with(['identity_cards', 'roles'])->get();
//    $books = User::query()->with(['identity_cards' => fn(Builder $q) => $q->orderBy('created_at', 'desc')])->get(['id', 'name']);
//    $books = User::query()->where('id',2)->with(['identity_cards' => function($query){
//        $query->select(['city','user_id','created_at'])->orderBy('created_at', 'desc');
//    }])->get(['name','id','email']);
//    $books = User::query()->with('identity_cards.city')->get();


//    $users = App\User::all();
//
//
//    foreach ($users as $user) {
//        //append the attribute of is_admin
//        $user->setAppends(['is_admin']);
//    }
//
//    $users = $users->map(function ($val) {
//
//        return [
//            'id'=>$val->id,
//            'name'=>$val->name
//        ];
//    });
//dd($data->toArray());
//    return $user->append('is_admin')->toArray();


});

//Route::get('/test','');



